/**
 * Created with JetBrains PhpStorm.
 * User: BGate
 * Date: 26/10/2012
 * Time: 12:21
 * To change this template use File | Settings | File Templates.
 */
var gamePageSelect = 0;
var TimeCount = 0;
var timing;
const BACK = 1,NEXT = 4, RESET = 3, PRE = 2;
// Hang so chi kieu cua InGameBut
/**
 *  Object chua vi tri
 * @param i
 * @param j
 */

function position(i,j){
    this.x = i;
    this.y = j;
    this.tran = i*cellN + j;
}
var colorSet = new Array(
    "null",
    "#0000ff",
    "#ff0000",
    "#00ffff",
    "#ff7f00",
    "#ff00ff",
    "#eeee00",
    "#008000",
    "#a52a2a"
);

var roadColorSet = new Array(
    "null",
    "#383872",
    "#723838",
    "#387272",
    "#725538",
    "#723877",
    "#6e6e38",
    "#385538",
    "#5e4242"
);


/*var hasRoad = new Array(); //Quan ly xem mot cap diem co Road hay chua
 for(i=1;i<9;i++){
 hasRoad[i]=0; //khoi tao
 }*/
var isClick = false;
var currentColor;
/**
 * Doi tuong chua paths cua moi Points
 * Chua cac cells trong path va
 * la doi tuong khoi tao va destroy() Road  ->>> Road phai viet lai
 */
function flow(){
    this.arr = new Array(); //Chua vi tri cac cell
    this.roads = new Array();// chua Road
    // truoc khi add position, cell phai duoc cap nhat cell.cC truoc

    this.isHas = function(position){
        for(var i=0;i<this.arr.length;i++){
            var temp = this.arr[i];
            if(temp.x == position.x && temp.y== position.y){
                return true;
            }
        }
        return false;
    }
    this.add = function(position){
        if(this.isHas(position)) return;
        this.arr.push(position);
        map[tran(position)].isRoad = true;
        map[tran(position)].color(roadColorSet[map[tran(position)].cC]);
        if(this.arr.length>=2){
            var temp_n = this.arr.length;
            this.roads[this.arr.length-2] = Crafty.e("DOM, 2D, Road")
                .road(this.arr[temp_n-1],this.arr[temp_n - 2],map[tran(position)].cC);
            if(this.roads[this.arr.length - 2] == null){
                this.roads.length = this.roads.length - 1;
                isClick = false;
                this.arr.pop();
                map[tran(position)].color("#e0bd44");
                map[tran(position)].isRoad = false;
                if(this.arr.length==1) this.clearAll();
                /**
                 * Truong hop khong tao duoc road;
                 */
            }
            return this;
        }
    }

    this.clearAll = function(){
        if(this.roads.length!=0){
            for(var i=0;i<this.roads.length;i++){
                this.roads[i].destroy();
            }
            this.roads.length = 0;
        }

        if(this.arr.length!=0){
            for(var i=0;i<this.arr.length;i++){
                map[tran(this.arr[i])].isRoad = false;

                map[tran(this.arr[i])].color("#e0bd44");
            }
            this.arr.length = 0;
        }
    }

    this.clearFromIn = function(position){
        for(var i=0;i<this.arr.length;i++){
            var temp = this.arr[i];
            if(temp.x == position.x && temp.y== position.y){
                break;
            }
        }
        if(i==1){
            this.clearAll();
            return;
        }
        for(var j=i;j<this.arr.length;j++){
            map[tran(this.arr[j])].isRoad = false;
            map[tran(this.arr[j])].color("#e0bd44");
        }
        for(var j=i-1; j<this.roads.length ; j++){
            this.roads[j].destroy();
        }

        this.roads.length = i-1;
        this.arr.length = i;
    }

    this.clearFrom = function(position){
        for(var i=0;i<this.arr.length;i++){
            var temp = this.arr[i];
            if(temp.x == position.x && temp.y== position.y){
                break;
            }
        }
        for(var j=i+1;j<this.arr.length;j++){
            map[tran(this.arr[j])].isRoad = false;
            map[tran(this.arr[j])].color("#e0bd44");
        }
        for(var j=i; j<this.roads.length ; j++){
            this.roads[j].destroy();
        }

        this.roads.length = i;
        this.arr.length = i+1;
    }
}

/**
 * Quan ly Cac Cell trong mot path
 * Cell duoc luu boi cặp số i,j
 * Mot Cell chi duoc nam trong mot Path
 * khi no nam trong Path thì Cell.isRoad = true
 * @type {Array}
 */
var Path = new Array();
for(var i=0;i<colorSet.length;i++){
    Path[i] = new flow();

}
resetPath = function(){
    for(var i=0;i<Path.length;i++){
        Path[i].clearAll();
    }
}

var compressMaps = new Array();

compressMaps[6] = new Array();
compressMaps[8] = new Array();
compressMaps[10] = new Array();

compressMaps[6][0] = [0,19,18,26,30,28,27,22,23,35,17,9,8,4,10,5];
compressMaps[6][1] = [0,14,6,31,7,26,20,32,34,28,22,5,4,16,15,3];
compressMaps[6][2] = [4,16,9,5,17,20,26,35,34,33,32,13,18,0,1,7];
compressMaps[6][3] = [15,14,20,29,27,21,23,4,3,0,6,13,12,31,25,19];
compressMaps[6][4] = [6,21,14,25,20,35,27,11,22,5,3,0,12,24,30,31];
compressMaps[6][5] = [0,22,8,32,7,31,30,6,4,5,17,28,35,33,-1,-1];
compressMaps[6][6] = [16,14,21,25,35,31,30,7,19,20,6,8,17,5,-1,-1];
compressMaps[6][7] = [16,22,27,0,19,2,8,3,17,35,33,30,-1,-1,-1,-1];
compressMaps[6][8] = [0,25,30,26,33,29,28,23,20,15,16,10,9,2,3,5];
compressMaps[6][9] = [0,25,19,32,33,13,7,8,9,5,10,22,23,35,-1,-1];
compressMaps[6][10] = [7,9,13,15,20,14,0,35,-1,-1,16,1,5,23,22,29];
compressMaps[6][11] = [0,5,1,13,21,3,2,14,19,20,27,4,-1,-1,-1,-1];
compressMaps[6][12] = [0,14,7,13,24,30,32,33,34,5,28,10,15,4,-1,-1];
compressMaps[6][13] = [0,10,5,20,16,6,12,14,18,24,32,29,28,26,-1,-1];
compressMaps[6][14] = [0,31,25,33,34,27,19,17,16,1,8,10,11,2,-1,-1];
compressMaps[6][15] = [0,17,10,18,24,34,35,16,15,28,21,20,-1,-1,-1,-1];
compressMaps[6][16] = [12,21,19,25,20,35,27,23,22,11,5,10,9,0,6,8];
compressMaps[6][17] = [6,9,12,13,14,35,27,5,22,4,3,0,-1,-1,-1,-1];

compressMaps[8][0] = [0,14,7,8,16,63,55,54,53,20,26,29,36,28,-1,-1];
compressMaps[8][1] = [19,18,17,54,56,63,48,5,9,11,26,55,27,22,23,7];
compressMaps[8][2] = [0,7,8,15,11,19,17,18,12,13,40,63,41,48,42,62];
compressMaps[8][3] = [0,7,12,36,40,47,17,18,13,30,16,8,48,62,49,63];
compressMaps[8][4] = [0,63,49,14,50,51,55,45,33,4,3,1,2,18,-1,-1];
compressMaps[8][5] = [16,22,24,31,30,53,54,34,42,45,23,5,4,0,8,12];
compressMaps[8][6] = [8,54,0,7,23,29,20,60,-1,-1,19,26,27,56,57,50];
compressMaps[8][7] = [16,55,24,63,54,34,25,42,35,36,23,7,4,8,0,3];
compressMaps[8][8] = [9,55,0,39,4,23,6,15,8,63,24,61,52,49,42,33];
compressMaps[8][9] = [0,6,23,7,1,46,21,5,12,3,16,63,52,50,34,25];

compressMaps[10][0] = [10,78,0,38,7,99,88,24,20,23,30,96,95,70,40,64];
compressMaps[10][1] = [0,78,1,79,3,47,5,16,27,59,20,42,53,64,60,98];
compressMaps[10][2] = [30,7,17,15,25,11,12,22,40,45,50,98,88,95,94,64];
compressMaps[10][3] = [90,60,50,11,12,83,82,27,33,37,36,46,45,55,-1,-1];
compressMaps[10][4] = [0,99,1,59,49,72,57,56,65,5,44,13,43,2,42,32];
compressMaps[10][5] = [0,17,65,72,61,35,80,86,77,67,58,9,48,38,28,7];



var pointMap = new Array();
/**
 * Quản lý vị trí ban đầu của các Point
 * Dùng để khởi tạo và lưu lại map
 */

var pointMapSets = 6;
/*pointMapSets[6] = new Array();
pointMapSets[8] = new Array();
pointMapSets[10] = new Array();*/

var pointMaps = new Object();
pointMaps.length = function(){
    return compressMaps[cellN].length;
}
pointMaps.getMap = function(gameGet){
    if(gameGet<compressMaps[cellN].length){


        var returnMap = new Array();
        for(var i=0; i<pointMaps.length;i++){
            returnMap[i] = 0;
        }
        for(i = 0; i< compressMaps[cellN][gameGet].length;i++){
            if(compressMaps[cellN][gameGet][i] != -1){
                returnMap[compressMaps[cellN][gameGet][i]] = Math.floor(i/2)+1;
            }
        }
        return returnMap;
    }
    return null;

}

mapSelect = 0;

/*function mapEtract(){
    for(var i=0; i < compressMaps[6].length ; i++){
        pointMapSets[6][i] = new Array();
        for(var j=0 ; j<36; j++){
            pointMapSets[6][i][j] = 0;
        }
        for(var n = 0; n < compressMaps[6][i].length; n++){
            if(compressMaps[6][i][n] != -1){
                pointMapSets[6][i][compressMaps[6][i][n]] = Math.floor(n/2)+1;
            }
        }
    }
    for(var i=0; i < compressMaps[8].length ; i++){
        pointMapSets[8][i] = new Array();
        for(var j=0 ; j<64; j++){
            pointMapSets[8][i][j] = 0;
        }
        for(var n = 0; n < compressMaps[8][i].length; n++){
            if(compressMaps[8][i][n] != -1){
                pointMapSets[8][i][compressMaps[8][i][n]] = Math.floor(n/2)+1;
            }
        }
    }
    for(var i=0; i < compressMaps[10].length ; i++){
        pointMapSets[10][i] = new Array();
        for(var j=0 ; j<100; j++){
            pointMapSets[10][i][j] = 0;
        }
        for(var n = 0; n < compressMaps[10][i].length; n++){
            if(compressMaps[10][i][n] != -1){
                pointMapSets[10][i][compressMaps[10][i][n]] = Math.floor(n/2)+1;
            }
        }
    }
}*/


/**
 * Mang cac  Cell, Mang 1 chieu
 * @type {Array}
 */
var map = new Array();
checkGameEnd = function(){
    for(var i = 0;i<cellN*cellN;i++){
        if( !(map[i].isPoint) && !(map[i].isRoad) ){
            return false
        }
        if(pointMap[i]){
            var a = Math.floor(i/cellN);
            var b = i - a*cellN;
            if(! (Path[map[i].cC].isHas(new position(a,b)))){
                return false;
            }
        }
    }
    return true;
}

function tran(myPos){
    return (myPos.x*cellN + myPos.y);
}

var played;

function loadPlayedMap(){
    if(typeof(Storage)!==undefined){
        played = new Array();
        played[6]=new Array();
        for(var i = 0 ; i < compressMaps[6].length ; i++){
            played[6][i] = 0;
        }
        played[8]=new Array();
        for(var i = 0 ; i < compressMaps[8].length ; i++){
            played[8][i] = 0;
        }
        played[10]=new Array();
        for(var i = 0 ; i < compressMaps[10].length ; i++){
            played[10][i] = 0;
        }

        if(localStorage.flowPlayed){
            //played = new Array();
            var txt = localStorage.flowPlayed;
            var splited = txt.split(':');
            var subSplited = splited[0].split(',');

           // played[6] = new Array();
            for(var i = 0; i < subSplited.length; i++){
                played[6][i] = subSplited[i];
            }

            subSplited = splited[1].split(',');
            //played[8] = new Array();
            for(var i = 0; i < subSplited.length; i++){
                played[8][i] = subSplited[i];
            }

            subSplited = splited[2].split(',');
            //played[10] = new Array();
            for(var i = 0; i < subSplited.length; i++){
                played[10][i] = subSplited[i];
            }
        }
    }
}
function savePlayed(){
    var txt = "";
    txt += played[6][0];
    for(var i=1; i<played[6].length; i++){
        txt = txt + ','+ played[6][i];
    }
    txt += ':';
    txt += played[8][0];
    for(var i=0; i<played[8].length; i++){
        txt = txt + ','+ played[8][i];
    }
    txt += ':';

    txt += played[10][0];
    for(var i=0; i<played[10].length; i++){
        txt = txt + ","+ played[10][i];
    }
    txt.length = txt.length - 1 ;

    localStorage.flowPlayed = txt;

}
var flowH,flowW,cellN,baseX,baseY,cellSize,isWitdhMode;
const GAMEWITH = 480, GAMEHEIGHT = 640;

window.onload = function(){
    window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
    Crafty.init(GAMEWITH, GAMEHEIGHT);
    //mapEtract();
    loadPlayedMap();
    flowH = GAMEHEIGHT;
    flowW = GAMEWITH;
    Crafty.scene("Load");

}

console.log("mainfile");